package com.launch.wlfw.controller;

import com.github.pagehelper.PageInfo;
import com.launch.wlfw.common.Result;
import com.launch.wlfw.pojo.BookInfo;
import com.launch.wlfw.pojo.TypeInfo;
import com.launch.wlfw.service.BookInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@Controller
public class BookInfoController {

    @Resource
    BookInfoService bookInfoService;

    // 页面
    @RequestMapping("/bookInfoIndex")
    public String bookIndex(){
        return "book/bookIndex";
    }


    // 数据
    @RequestMapping("/bookAll")
    @ResponseBody
    public Result<PageInfo<BookInfo>> bookAll(BookInfo bookInfo ,
                                    @RequestParam(value = "page")Integer page,
                                    @RequestParam(value = "limit")Integer limit){
        return Result.success(bookInfoService.getListBook(bookInfo , page , limit));
    }

    // 页面
    @RequestMapping("/bookAdd")
    public String bookAdd(){
        return "book/bookAdd";
    }

    // 数据
    @RequestMapping("/addBookSubmit")
    @ResponseBody
    public Result<Integer> addBook(BookInfo bookInfo){
        return Result.success(bookInfoService.addBook(bookInfo));
    }

    @RequestMapping("/deleteBook")
    @ResponseBody
    public Result<Integer> deleteBookById(Integer ids){
       return Result.success(bookInfoService.deleteBook(ids));
    }

    @RequestMapping("/findAllList")
    @ResponseBody
    public Result<List<TypeInfo>> findAllList(){
        return Result.success(bookInfoService.findAllList());
    }

    @RequestMapping("/queryBookInfoById")
    public String updateBook( Integer id, Model model){
        model.addAttribute("info", bookInfoService.getBookById(id));
        return "book/updateBook";
    }

    @RequestMapping("/updateBookSubmit")
    @ResponseBody
    public Result<Integer> updateBookSubmit(@RequestBody BookInfo book) {
        return Result.success(bookInfoService.updateBook(book));
    }
}
